package com.wangp.mywrite.coding;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author: wp
 * @date: 2023/5/19 14:12:41
 * @desc:
 **/
public class NumTilePossibilities {

    public int numTilePossibilities(String tiles) {
        Map<Character, Integer> count = new HashMap<>();
        for (char c : tiles.toCharArray()) {
            count.put(c, count.getOrDefault(c, 0) + 1);
        }
        Set<Character> characters = new HashSet<>(count.keySet());
        return dfs(tiles.length(), count, characters) - 1;
    }

    private int dfs(int length, Map<Character, Integer> count, Set<Character> characters) {
        if (length == 0) {
            return 1;
        }
        int res = 1;
        for (Character c : characters) {
            if (count.get(c) > 0) {
                count.put(c, count.get(c) - 1);
                res += dfs(length - 1, count, characters);
                count.put(c, count.get(c) + 1);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new NumTilePossibilities().numTilePossibilities("AAB"));
    }
}
